package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    /**
     * 查询所有待审核或拒绝的游记
     * @param qo 查询条件
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQuery qo,Model model){
        if (qo.getState() == null) {
            qo.setState(Travel.STATE_AUDIT);
        }
        qo.setOrderBy("tr.lastUpdateTime desc");
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/list";
    }

    /**
     * 改变游记的状态
     * @param travel 游记信息,包括id,状态等
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Travel travel){
        travelService.changeState(travel);
        return new JSONResult();
    }

    /**
     * 查询已发布的游记
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQuery qo,Model model){
        qo.setOrderBy("tr.releaseTime desc");
        qo.setState(Travel.STATE_RELEASE);
        qo.setIsPublic(true);
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/releaseList";
    }
}
